package com.chain.voter;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.chain.voter.record.FavouriteShare;
import com.naschain.naspay.NasPayManager;
import com.naschain.naspay.nebulas.utils.Util;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.chain.voter.MainActivity.getCurrentDate;

/**
 * Created by baoyunfeng on 2018/6/23.
 */

public class PublishActivity extends Activity {

    @BindView(R.id.back)
    View back;
    @BindView(R.id.question)
    EditText question;
    @BindView(R.id.publish)
    View publish;
    @BindView(R.id.anwser)
    EditText anwser;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish);
        ButterKnife.bind(this);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String questionStr = question.getText().toString();
                if (!TextUtils.isEmpty(questionStr) && !TextUtils.isEmpty(anwser.getText().toString
                        ())) {

                    String unicode = Util.getRandomCode(32);

                    NasPayManager.createCallBuilder(PublishActivity.this)
                            .setFunctionName("save")
                            .setArgs(new String[]{questionStr, getCurrentDate(), getCurrentDate(),
                                    anwser.getText().toString()})
                            .setSerialNumber(unicode)
                            .build();


                    FavouriteShare.addPendingPayRecord(PublishActivity.this,unicode,"发布问题："+questionStr);
                    Toast.makeText(PublishActivity.this, "投票正在发布~~请稍后刷新页面~~", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(PublishActivity.this, "问题和回答都要填写哟~", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
